// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ResMgr from "./ResMgr";

const {ccclass, property} = cc._decorator;

export default class PanelManager extends cc.Component {

    private static Instance: PanelManager = null;
    private ui_map:any = {};


    onLoad () 
    {
        PanelManager.Instance = this;
    }

    public static getInstace()
    {
        return PanelManager.Instance;
    }



    showPanel(panel_name)
    {
        var parent = cc.find("Canvas").getChildByName("panel");
        var prefab = ResMgr.Instance.getAsset("GUI", panel_name);
        var item = null;
        if (prefab) 
        {
            item = cc.instantiate(prefab);
            parent.addChild(item);
        }
        this.ui_map[panel_name] = item;
        return item;
    }

    removePanel(panel_name)
    {
        if (this.ui_map[panel_name]) 
        {
            this.ui_map[panel_name].removeFromParent();
            this.ui_map[panel_name] = null;
        }

    }

    removeAllPanel()
    {
        for (var key in this.ui_map) {
            if (this.ui_map[key]) {
                this.ui_map[key].removeFromParent();
                this.ui_map[key] = null;
            }
        }
    }

    // update (dt) {}
}
